<?php  
    session_start();
    require_once('userlevel.php'); //Require/include the permissions file
    require_once('template.php'); //Require/include the template file
    require_once('functions.php'); //Require/include the functions file
    require_once('db.php'); //Require/include the file with the database connection information
     
    if (!$_SESSION['user_loggedin']) {
        die ("Not logged in... <script>document.location.href='login.php'</script>");
    } elseif (!($_SESSION['userlevel'] >= $userlevel['history'])) {
        die (errorpage ("Your user level is not high enough to use this feature!", 'Update Filename'));
    } else {
        $id = mysql_escape_string($_GET['id']);
         
        //Connect to MySQL and connect to the database using values provided by db.php
        $db = new my_db;
        $db->query("SELECT * FROM trackedfiles WHERE id='$id'");
        $db->next_record();
        $filename = mysql_escape_string(strip_tags($db->f('filename')));
        $filepath = mysql_escape_string(strip_tags($db->f('filepath')));
        $filetype = $db->f('filetype');
        $mime = explode("/", $filetype);
         
        $db->query("SELECT * FROM history WHERE filename='$filename' AND filepath='$filepath' ORDER BY revdate DESC");
        template_headtag("History of $filename");
        template_header();
        template_left();
        if ($db->nf() > 0) {
             
             
            echo "<div class='pageheadertext'>History of $filename in</div>";
            echo "<div class='subheadertext'>$filepath</div><br/>";
            $time = date("Y-m-j g:i");            echo "<div class='servertime'>Server time : $time";            echo "</div>";
            echo "Revisions available for <strong>$filename</strong><br /><br />";
            
            echo "<div align='center'>";
            echo "<form name='form1' method='post' action='diff.php?id=$id'>";
            echo "<table class='table'>";
             
            $run1 = True;
            $run2 = True;
             
            // keep getting the next row until there are no more to get
            while ($db->next_record()) {
                // Print out the contents of each row into a table
                $historyid = strip_tags($db->f('id'));
                $user = strip_tags($db->f('user'));
                $revdate = strip_tags($db->f('revdate'));
                $revhash = strip_tags($db->f('revhash'));
                $revsummary = strip_tags($db->f('revsummary'));
                $filetype = strip_tags($db->f('filetype'));
                if ($rowstyle == 'evenrow') {
                    $rowstyle = 'oddrow';
                } else {
                    $rowstyle = 'evenrow';
                }
                echo "<tr class='$rowstyle'>";
                if ($row[revhash] == '00000000000000000000000000000000') {
                    echo "<td>
                        </td>
                        <td>
                        </td>
                        <td>";
                } else {
                    if ($mime[0] == "text" AND $db->nf() > 1) {
                        if ($run1) {
                            echo "<td>
                                </td>";
                            echo "<td>
                                <input type='radio' name='id2' value='$revhash' checked='checked' />
                                </td>";
                             
                            $run1 = False;
                             
                        } elseif($run2) {
                            echo "<td>
                                <input type='radio' name='id1' value='$revhash' checked='checked' />
                                </td>";
                            echo "<td>
                                <input type='radio' name='id2' value='$revhash'/>
                                </td>";
                             
                            $run2 = False;
                             
                        } else {
                            echo "<td>
                                <input type='radio' name='id1' value='$revhash'/>
                                </td>";
                            echo "<td>
                                <input type='radio' name='id2' value='$revhash'/>
                                </td>";    
                        }
                    }
                }
                echo "<td>$revdate</td>";
                echo "<td>";
                if ($user == "Unknown") {
                    echo "<A href='setuser.php?id=$historyid&amp;fileid=$id'>$user</a>";
                } else {
                    echo "$user";
                }
                echo "</td>
                    <td>$revsummary</td><td width=20><a href='editsummary.php?id=$historyid&amp;fileid=$id'><span class='tinytext'><img src='images/tables/editsummary.png' width='16' height='16' border='0' alt='Edit Summary' title='Edit Summary'></a>";
                echo "</td><td width='20'>";
                if ($row[revhash] !== '00000000000000000000000000000000') {
                    echo "<a href='view.php?id=$id&amp;rev=$revhash' title='View file'><img src='images/tables/view.png' width='16' height='16' border='0' alt='View'/></a>";
                }
                echo "</td></tr>";
                 
                 
            }
            if ($mime[0] == "text" AND $db->nf() > 1) {
                echo "<tr><td colspan='2'><input type='submit' name='Submit' value='Compare' /></td></tr>";
            }
            echo "</table></form></div><br/>";
        } else {
            errorbox('No history found!');
        }
        template_footer();
    }
?>
